<%@page import="models.User"%>
<%@include file="../template/configuration.jsp" %>
<%@page import="models.PrivateMessage"%>
<%@page import="dao.PrivateMessageDao"%>
<%@page import="java.util.List"%>
<%@page import="classes.HibernateUtil"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
    if (request.getParameter("page") != null && request.getParameter("folder") != null) {
        HibernateUtil hu = new HibernateUtil();
        PrivateMessageDao cd = new PrivateMessageDao(hu.openSession());
        User user = (User) request.getSession().getAttribute("user");
        List<PrivateMessage> private_messages = cd.getPagination(Integer.parseInt(request.getParameter("page")), request.getParameter("folder"), user.getMember_id());
        pageContext.setAttribute("private_messages", private_messages);
        pageContext.setAttribute("folder", request.getParameter("folder"));
    }
%>
<table class="overview">
    <tr>
        <th class="single-icon"></th>
        <th>Subject</th>
        <th>
            <c:choose>
                <c:when test="${folder == 'outbox'}">
                    To
                </c:when>
                <c:when test="${folder == 'trash'}">
                    Folder
                </c:when>
                <c:otherwise>
                    From
                </c:otherwise>
            </c:choose>
        </th>
        <th class="date">Date</th>
        <th class="manage<c:if test="${folder != 'outbox'}">-large</c:if>">Actions</th>
    </tr>
    <c:set var="i" value="0"/>
    <c:forEach items="${private_messages}" var="message">
        <c:set var="i" value="${i + 1}"/>
        <tr class="<c:choose><c:when test="${i % 2 == 0}">even</c:when><c:otherwise>odd</c:otherwise></c:choose>">
            <td>
                <a href="${url}ComposePrivateMessage?message=${message.id}&action=view" title="View">
                    <c:choose>
                        <c:when test="${message.status == 1}">
                            <img src="${url}images/mail-b-closed.png" />
                        </c:when>
                        <c:otherwise>
                            <img src="${url}images/mail-b-open.png" />
                        </c:otherwise>
                    </c:choose>
                </a>
            </td>
            <td <c:if test="${message.status == 1}">style="color: #1a344b; font-weight: bold;"</c:if>>
                <a href="${url}ComposePrivateMessage?message=${message.id}&action=view" title="View">${message.subject}</a>
            </td>
            <td>
                <c:choose>
                    <c:when test="${folder == 'outbox'}">
                        ${message.receiver.firstname} ${message.receiver.insertion} ${message.receiver.lastname}
                    </c:when>
                    <c:when test="${folder == 'trash'}">
                        <c:choose>
                            <c:when test="${message.sender.member_id == sessionScope.userdetails.id}">
                                Outbox
                            </c:when>
                            <c:otherwise>
                                Inbox
                            </c:otherwise>
                        </c:choose>
                    </c:when>
                    <c:otherwise>
                        ${message.sender.firstname} ${message.sender.insertion} ${message.sender.lastname}
                    </c:otherwise>
                </c:choose>
            </td>
            <td>${message.date}</td>
            <td>
                <a href="${url}ComposePrivateMessage?message=${message.id}&action=view" title="View"><img src="${url}images/view.png" /></a>
                <c:if test="${folder == 'inbox'}">
                    <a href="${url}ComposePrivateMessage?message=${message.id}&action=reply" title="Reply"><img src="${url}images/mail-reply.png" /></a>
                </c:if>
                <c:if test="${folder == 'trash'}">
                    <a href="${url}ComposePrivateMessage?message=${message.id}&action=revert" title="Revert"><img src="${url}images/mail-revert.png" /></a>
                </c:if>
                <a href="${url}ComposePrivateMessage?message=${message.id}&action=delete" title="Remove" class="remove"><img src="${url}images/remove.png" /></a>
                <c:choose>
                    <c:when test="${folder == 'trash'}">
                        <span class="info">this message definitely from <strong>${message.sender.firstname} ${message.sender.insertion} ${message.sender.lastname}</strong><br />(This action cannot not be undone)</span>
                    </c:when>
                    <c:otherwise>
                        <span class="info">this message from <strong>${message.sender.firstname} ${message.sender.insertion} ${message.sender.lastname}</strong></span>
                    </c:otherwise>
                </c:choose>
            </td>
        </tr>
    </c:forEach>
</table>